package com.zf.zf_demo.controller;

import com.zf.zf_demo.Response;
import com.zf.zf_demo.dao.Student;
import com.zf.zf_demo.dto.StudentDTO;
import com.zf.zf_demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;
    //通过id找
    @GetMapping("/student/{id}")
    public Response<StudentDTO> getStudentById(@PathVariable int id) {
       return Response.newSuccess(studentService.getStudentById(id));
    }
    //添加
    @PostMapping("/student")
    public Response<Integer> addNewStudent(@RequestBody StudentDTO studentDTO){
        //@RequestBody接受前端json
        return  Response.newSuccess(studentService.addNewStudent(studentDTO));
    }
    //通过id删除
    @DeleteMapping("/student/{id}")
    public void deleteStudentById(@PathVariable int id){
        studentService.deleteStudentById(id);
    }
    //通过id更新
    @PutMapping("/student/{id}")
    public Response<StudentDTO> updateStudentById(@PathVariable int  id,
                                                  @RequestParam(required = false)String name,
                                                  @RequestParam(required = false)String email
                                                  ){
        return Response.newSuccess(studentService.updateStudentById(id,name,email));
    }
}
//